/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import WebFeeds 1.0
import "UIConstants.js" as UIConstants
import "Utils.js" as Utils

CommonPageContents {
    id: root
    property alias cacheBuffer: listView.cacheBuffer
    property alias emptyListText: emptyLabel.text
    property alias delegate: visualModel.delegate
    property bool enabled: true
    property Component pageHeader: CommonPageHeader {
        id: defaultPageHeader
        titleText: root.rootItem.title
        titleColor: "white"
        titleFont: UIConstants.FONT_FAMILY_LIGHT
        backgrounColor: "darkorange"
        Separator {
            id: separator
            anchors.bottom: parent.bottom
        }
    }

    function indexForRow(row) {
        return visualModel.modelIndex(row)
    }
    function itemForRow(row) {
        return webFeedsApp.pageItemModel.item(indexForRow(row));
    }

    Loader {
        id: pageHeaderLoader
        anchors.top: parent.top
        anchors.left: parent.left
        width: parent.width
        sourceComponent: root.pageHeader
    }
    Item {
        id: contentItem
        anchors.top: pageHeaderLoader.bottom
        anchors.bottom: parent.bottom
        width: parent.width
        ListView {
            id: listView
            property bool isEmpty: (listView.currentItem == null)
            anchors.fill: parent
            visible: !root.busy && !listView.isEmpty
            clip: true
            model: root.enabled ? visualModel : undefined
            Component.onDestruction: listView.model = undefined;
            ScrollDecorator {
                id: scrollDecorator
                flickableItem: listView
            }
        }
        BusyIndicator {
            anchors.centerIn: parent
            running: root.busy
            visible: root.busy
            style: BusyIndicatorStyle {
                size: "large"
            }
        }
        Label {
            id: emptyLabel
            visible: !root.busy && listView.isEmpty
            width: parent.width
            anchors.verticalCenter: parent.verticalCenter
            wrapMode: Text.Wrap
            font.pixelSize: UIConstants.FONT_XXXLARGE
            font.family: UIConstants.FONT_FAMILY_LIGHT
            horizontalAlignment: Text.AlignHCenter
            color: "gray"
        }
    }
    VisualDataModel {
        id: visualModel
        model: webFeedsApp.pageItemModel
        rootIndex: root.rootIndex
    }
}
